//
//  ScreenManager.cpp
//  SkyFight
//
//  Created by HoangLinh on 7/25/13.
//
//

#include "ScreenManager.h"

#include "HelloWorldScene.h"

#include "SinglePlayScreen.h"


// --------------------------- singleton -----------------------------

ScreenManager* ScreenManager::_sharedInstance = 0;

ScreenManager::ScreenManager()
{
    gameDirector = CCDirector::sharedDirector();
    this->calculateScaleFactor();
    CCLog("game data...");
    DataAccess::sharedDataAccess()->loadGameData();
    DataAccess::sharedDataAccess()->gameData.selectedChapter = 1;
    DataAccess::sharedDataAccess()->gameData.selectedLevel = 0;
    DataAccess::sharedDataAccess()->saveGameData();

    atexit(&purgeScreenManager);
}

void ScreenManager::purgeScreenManager()
{
    delete _sharedInstance;
    _sharedInstance = 0;
}

ScreenManager* ScreenManager::sharedScreenMan()
{
    if (_sharedInstance == 0) {
        _sharedInstance = new ScreenManager();
    }
    return _sharedInstance;
}

ScreenManager::~ScreenManager()
{
    
}

void ScreenManager::calculateScaleFactor()
{
	CCLog("calculate scale factor....");
    vector<string> searchPath;


#ifndef ANDROID
    char *fullPath;

#ifdef _WIN32
    fullPath = _fullpath(NULL, "..\\Resources\\", _MAX_PATH);
#else // Linux
    fullPath = realpath("../Resources/", NULL);
#endif

    searchPath.push_back(fullPath);
#endif

    char *imagePath, *menuPath, *effectPath, *effectImagePath;

    if (CCDirector::sharedDirector()->getWinSize().height < 480) {
        scaleFactor = 1;

        // load image path
#ifdef ANDROID
        imagePath = new char[50];
        strcpy(imagePath, "image/480x320");
#elif _WIN32
        imagePath = _fullpath(NULL, "..\\Resources\\image\\480x320\\", _MAX_PATH);
#else // Linux
        imagePath = realpath("../Resources/image/480x320/", NULL);
#endif

        // load effect image path
#ifdef ANDROID
        effectImagePath = new char[50];
        strcpy(effectImagePath, "effect/480x320");
#elif _WIN32
        effectImagePath = _fullpath(NULL, "..\\Resources\\effect\\480x320\\", _MAX_PATH);
#else // Linux
        effectImagePath = realpath("../Resources/effect/480x320/", NULL);
#endif

        // load menu image path
#ifdef ANDROID
        menuPath = new char[50];
        strcpy(menuPath, "menu/480x320");
#elif _WIN32
        menuPath = _fullpath(NULL, "..\\Resources\\menu\\480x320\\", _MAX_PATH);
#else // Linux
        menuPath = realpath("../Resources/menu/480x320/", NULL);
#endif

    } else {
        scaleFactor = 2;

        // load image path
#ifdef ANDROID
        imagePath = new char[50];
        strcpy(imagePath, "image/960x480");
#elif _WIN32
        imagePath = _fullpath(NULL, "..\\Resources\\image\\960x640\\", _MAX_PATH);
#else // Linux
        imagePath = realpath("../Resources/image/960x640/", NULL);
#endif

        // load effect image path
#ifdef ANDROID
        effectImagePath = new char[50];
        strcpy(effectImagePath, "effect/960x640");
#elif _WIN32
        effectImagePath = _fullpath(NULL, "..\\Resources\\effect\\960x640\\", _MAX_PATH);
#else // Linux
        effectImagePath = realpath("../Resources/effect/960x640/", NULL);
#endif

        // load menu image path
#ifdef ANDROID
        menuPath = new char[50];
        strcpy(menuPath, "menu/960x480");
#elif _WIN32
        menuPath = _fullpath(NULL, "..\\Resources\\menu\\960x640\\", _MAX_PATH);
#else // Linux
        menuPath = realpath("../Resources/menu/960x640/", NULL);
#endif

    }
    CCLog("scaleFactor: %d", scaleFactor);
    searchPath.push_back(imagePath);
    searchPath.push_back(effectImagePath);
    searchPath.push_back(menuPath);

    // effect path
#ifdef ANDROID
    effectPath = new char [50];
    strcpy(effectPath, "effect");
#elif _WIN32
    effectPath = _fullpath(NULL, "..\\Resources\\effect\\", _MAX_PATH);
#else // Linux
    effectPath = realpath("../Resources/effect/", NULL);
#endif

    searchPath.push_back(effectPath);

    // set searching path
    CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);

#ifdef CCOBJECT_DEBUG
    vector<string> myPaths = CCFileUtils::sharedFileUtils()->getSearchPaths();
    for (vector<string>::iterator it = myPaths.begin(); it != myPaths.end(); ++it) {
        string str = *it;
        CCLog("search path: %s", str.c_str());
    }
#endif

    // free path memory
#ifndef ANDROID
    free(fullPath);
#endif

    free(imagePath);
    free(effectImagePath);
    free(menuPath);
    free(effectPath);
}

// -------------------------- switch layer ----------------------------------
void ScreenManager::go(GameLayer *gameLayer)
{
	CCLog("go game layer....");
    currentLayer = gameLayer;

    CCScene *scene = CCScene::create();
    scene->addChild(gameLayer);
    gameLayer->parentScene = scene;
    gameLayer->release();

    gameDirector->getTouchDispatcher()->removeAllDelegates();
    if (gameDirector->getRunningScene() != NULL) {
        gameDirector->replaceScene(scene);
    } else {
        gameDirector->pushScene(scene);
    }

    gameLayer->loadGameLayer();
}

void ScreenManager::goHelloWorld()
{
    CCScene *helloScene = HelloWorld::scene();
    gameDirector->pushScene(helloScene);
}

void ScreenManager::goSinglePlay()
{
    currentLayer = NULL;
    SinglePlayScreen *singlePlayScreen = new SinglePlayScreen();    
    this->go(singlePlayScreen);
    singlePlayScreen->initGameLayer();
}
